package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishFlavorrMapper;
import com.itheima.reggie.service.DishFlavorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Xiao
 * @date 2021/9/26
 */
@Service
@Slf4j
public class DishFlavorServiceImpl extends ServiceImpl<DishFlavorrMapper, DishFlavor> implements DishFlavorService {
    /**
     * 存储口味信息
     * @param dishDto
     * @return
     */
    @Override
    public boolean saveFlavors(DishDto dishDto) {
        List<DishFlavor> flavors = dishDto.getFlavors();
        log.info("flavors:{}",flavors.toString());
        //通过增强for给每一个flavor元素设置dishId
        /*for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishDto.getId());
        }*/
        //通过流的形式给每一个flavor元素设置dishId
        flavors = flavors.stream().map((flavor) -> {
            flavor.setDishId(dishDto.getId());
            return flavor;
        }).collect(Collectors.toList());
        return this.saveBatch(flavors);
    }

    /**
     * 根据菜品id查询口味信息
     * @param dishId
     * @return
     */
    @Override
    public List<DishFlavor> queryFlavorsByDishId(Long dishId) {
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId, dishId);
        return this.list(wrapper);
    }

    /**
     * 根据菜品id删除对应口味数据
     * @param dishId
     * @return
     */
    @Override
    public boolean removeFlavors(Long dishId) {
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,dishId);
        return this.remove(wrapper);
    }

    /**
     * 根据菜品id批量删除口味数据
     * @param dishIds
     * @return
     */
    @Override
    public boolean removeFlavorsByDishIds(List<Long> dishIds) {
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        /*List<Long> list = new ArrayList<>();
        for (Long id : ids) {
            wrapper.eq(DishFlavor::getDishId, id);
            DishFlavor flavor = this.getOne(wrapper);
            list.add(flavor.getId());
        }
        return this.removeByIds(list);*/
        wrapper.in(DishFlavor::getDishId, dishIds);
        return this.remove(wrapper);
    }
}
